﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThermalEquipmentDesign.Model.CasingPipeSprinklers
{
    public class CasingPipeSprinklersModel
    {
        public CasingPipeSprinklersResult result = new CasingPipeSprinklersResult();
        public CasingPipeSprinklers casingpipesprinklers;

        /// <summary>
        /// Calculate Tube In Way
        /// </summary>
        /// <returns>Tube In Way</returns>
        private double TubesInWay()
        {
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            return Math.Round(n1,2);
        }

        /// <summary>
        /// Calculate Water Speed
        /// </summary>
        /// <returns>Water Speed</returns>
        private double WaterSpeed()
        {
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            return w;
        }

        /// <summary>
        /// Calculate Reynolds Value
        /// </summary>
        /// <returns>Reynolds Value</returns>
        private double ReynoldsValue()
        {
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            return Math.Round(Re,0);
        }

        /// <summary>
        /// Calculate Nusselt Value
        /// </summary>
        /// <returns>Nusselt Value</returns>
        private double NusseltValue()
        {
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            return Math.Round(Nu,2);
        }

        /// <summary>
        /// Calculate Heat Transfer Coefficient Salt
        /// </summary>
        /// <returns>Heat Transfer Coefficient Salt</returns>
        private double HeatTransferCoefficientSalt()
        {
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            return Math.Round(abi,0);
        }

        /// <summary>
        /// Calculate Heat Transfer Coefficient Cold
        /// </summary>
        /// <returns>Heat Transfer Coefficient Cold</returns>
        private double HeatTransferCoefficientCold()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double a = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.48);
            return Math.Round(a,0);
        }

        /// <summary>
        /// Calculate Heat Density Begin Boil
        /// </summary>
        /// <returns>Heat Density Begin Boil</returns>
        private double HeatDensityBeginBoil()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double Qfs = 16 * 1000000 * Math.Pow(0.00003, 0.35) * Math.Pow(tmp, 0.76) * Math.Pow(2.969, -0.43);
            return Math.Round(Qfs,0);
        }

        /// <summary>
        /// Calculate Heat Density Boil
        /// </summary>
        /// <returns>Heat Density Boil</returns>
        private double HeatDensityBoil()
        {
            double Qfi = Math.Pow(55, 2.7) * Math.Pow(2.969, 0.73) * Math.Pow(6.8, 2.7);
            return Qfi;
        }

        /// <summary>
        /// Calculate Heat Coefficient Average
        /// </summary>
        /// <returns>Heat Coefficient Average</returns>
        private double HeatCoefficientAverage()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            return Math.Round(aRi,0);
        }

        /// <summary>
        /// Calculate Heat Transfer Coefficient
        /// </summary>
        /// <returns>Heat Transfer Coefficient</returns>
        private double HeatTransferCoefficient()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            double Ki = 1 / (1 / aRi + 1 / abi + 0.0005);
            return Math.Round(Ki,0);
        }

        /// <summary>
        /// Calculate Heat Transfer Area
        /// </summary>
        /// <returns>Heat Transfer Area</returns>
        private double HeatTransferArea()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            double Ki = 1 / (1 / aRi + 1 / abi + 0.0005);
            double Fi = casingpipesprinklers.dbCapacity*1000 / (Ki * 6.8);
            return Math.Round(Fi,2);
        }

        /// <summary>
        /// Calculate Tubes Layout
        /// </summary>
        /// <returns>Tubes Layout</returns>
        private double TubesLayout()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            double Ki = 1 / (1 / aRi + 1 / abi + 0.0005);
            double Fi = casingpipesprinklers.dbCapacity / (Ki * 6.8);
            double layouttmp = Fi / (casingpipesprinklers.dbDiameter * 6 * casingpipesprinklers.dbBuocOng);
            double nc = 0.75 * Math.Pow(layouttmp, 1 / 3);
            return nc;
        }

        /// <summary>
        /// Calculate Casing Diameter
        /// </summary>
        /// <returns>Casing Diameter</returns>
        private double CasingDiameter()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            double Ki = 1 / (1 / aRi + 1 / abi + 0.0005);
            double Fi = casingpipesprinklers.dbCapacity / (Ki * 6.8);
            double layouttmp = Fi / (casingpipesprinklers.dbDiameter * 6 * casingpipesprinklers.dbBuocOng);
            double nc = 0.75 * Math.Pow(layouttmp, 1 / 3);
            double Di = nc * casingpipesprinklers.dbBuocOng;
            return Di;
        }

        /// <summary>
        /// Calculate Tube Length
        /// </summary>
        /// <returns>Tube Length</returns>
        private double TubeLength()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            double Ki = 1 / (1 / aRi + 1 / abi + 0.0005);
            double Fi = casingpipesprinklers.dbCapacity / (Ki * 6.8);
            double layouttmp = Fi / (casingpipesprinklers.dbDiameter * 6 * casingpipesprinklers.dbBuocOng);
            double nc = 0.75 * Math.Pow(layouttmp, 1 / 3);
            double Di = nc * casingpipesprinklers.dbBuocOng;
            double l = Di * 6;
            return l;
        }

        /// <summary>
        /// Calculate Total Tubes
        /// </summary>
        /// <returns>Total Tubes</returns>
        private double TotalTubes()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            double Ki = 1 / (1 / aRi + 1 / abi + 0.0005);
            double Fi = casingpipesprinklers.dbCapacity / (Ki * 6.8);
            double layouttmp = Fi / (casingpipesprinklers.dbDiameter * 6 * casingpipesprinklers.dbBuocOng);
            double nc = 0.75 * Math.Pow(layouttmp, 1 / 3);
            double Di = nc * casingpipesprinklers.dbBuocOng;
            double l = Di * 6;
            double nt = Fi / (Math.PI * casingpipesprinklers.dbDiameter * l);
            return nt;
        }

        /// <summary>
        /// Calculate Ways
        /// </summary>
        /// <returns>Ways</returns>
        private double Ways()
        {
            double tmp = casingpipesprinklers.dbBuocOng / casingpipesprinklers.dbOuterDiameter;
            double aRi = 9800 * Math.Pow(0.00003, 0.22) * Math.Pow(tmp, 0.18) * casingpipesprinklers.dbOuterDiameter / casingpipesprinklers.dbDiameter;
            double n1 = (casingpipesprinklers.dbCapacity * 4) / (1.5 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double w = (casingpipesprinklers.dbCapacity * 4) / (n1 * 2.91 * 1232 * 4 * Math.PI * casingpipesprinklers.dbDiameter * casingpipesprinklers.dbDiameter);
            double Re = w * casingpipesprinklers.dbDiameter / (4.347 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipesprinklers.dbDiameter;
            double Ki = 1 / (1 / aRi + 1 / abi + 0.0005);
            double Fi = casingpipesprinklers.dbCapacity / (Ki * 6.8);
            double layouttmp = Fi / (casingpipesprinklers.dbDiameter * 6 * casingpipesprinklers.dbBuocOng);
            double nc = 0.75 * Math.Pow(layouttmp, 1 / 3);
            double Di = nc * casingpipesprinklers.dbBuocOng;
            double l = Di * 6;
            double nt = Fi / (Math.PI * casingpipesprinklers.dbDiameter * l);
            double z = nt / n1;
            return z;
        }

        /// <summary>
        /// Calculate Casing Pipe Sprinkler
        /// </summary>
        /// /// <param name="con">CasingPipeSprinkler object</param>
        /// <returns>CasingPipeSprinklersResult object</returns> 
        public CasingPipeSprinklersResult ImplementCalculation(CasingPipeSprinklers cps)
        {
            casingpipesprinklers = cps;
            result.dbCasingDiameter = CasingDiameter();
            result.dbHeatDensityBeginBoil = HeatDensityBeginBoil();
            result.dbHeatDensityBoil = HeatDensityBoil();
            result.dbHeatTransferArea = HeatTransferArea();
            result.dbHeatTransferCoefficient = HeatTransferCoefficient();
            result.dbHeatTransferCoefficientCold = HeatTransferCoefficientCold();
            result.dbHeatTransferCoefficientSalt = HeatTransferCoefficientSalt();
            result.dbHeatCoefficientAverage = HeatCoefficientAverage();
            result.dbNusseltValue = NusseltValue();
            result.dbReynoldsValue = ReynoldsValue();
            result.dbTotalTubes = TotalTubes();
            result.dbTubeLength = TubeLength();
            result.dbTubesInWay = TubesInWay();
            result.dbTubesLayout = TubesLayout();
            result.dbWaterSpeed = WaterSpeed();
            result.dbWays = Ways();
            return result;
        }
    }
}
